package com.joejag.mavenstats.client.views.forms.project;

import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.Ext;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.LayoutDialog;
import com.gwtext.client.widgets.LayoutDialogConfig;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.ContentPanel;
import com.gwtext.client.widgets.layout.LayoutRegionConfig;
import com.joejag.mavenstats.client.dto.ProjectDTO;

public class ProjectDTODialog extends LayoutDialog
{
  private final ProjectDTOForm projectForm;

  public ProjectDTODialog(final ProjectDTOForm projectForm)
  {
    super(new LayoutDialogConfig()
    {
      {
        setModal(false);
        setWidth(700);
        setHeight(300);
        setShadow(true);
        setResizable(false);
        setClosable(true);
        setProxyDrag(true);
        setTitle(projectForm.getText());
      }
    }, center);

    this.projectForm = projectForm;

    makeContainer();
  }

  private void makeContainer()
  {
    final BorderLayout layout = this.getLayout();
    layout.beginUpdate();

    ContentPanel signInPanel = new ContentPanel(Ext.generateId(), projectForm.getText());
//    final ProjectDTOForm projectForm = new ProjectDTOForm(project, text);
    VerticalPanel signInWrapper = new VerticalPanel()
    {
      {
        setSpacing(30);
        setWidth("100%");
        setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
      }
    };
    signInWrapper.add(projectForm);

    signInPanel.add(signInWrapper);
    layout.add(LayoutRegionConfig.CENTER, signInPanel);

    layout.endUpdate();

    final ProjectDTODialog dialog = this;
    final com.gwtext.client.widgets.Button signInBtn = this.addButton(projectForm.getText());
    signInBtn.addButtonListener(new ButtonListenerAdapter()
    {
      public void onClick(Button button, EventObject eventObject)
      {
        if (projectForm.isValid())
        {
          dialog.hide();
          projectForm.performSomeAction(projectForm.getProject());
          projectForm.reset();
        }
      }
    });

    this.setTitle(projectForm.getText());
    signInBtn.show();
  }

  private static LayoutRegionConfig center = new LayoutRegionConfig()
  {
    {
      setAutoScroll(true);
      setTabPosition("top");
      setCloseOnTab(true);
      setAlwaysShowTabs(false);
    }
  };
}
